<!--
 * @Author: Seon.Hu SeonHu@126.com
 * @Date: 2023-06-16 15:13:21
 * @LastEditors: Seon.Hu SeonHu@126.com
 * @LastEditTime: 2023-11-20 10:34:32
 * @FilePath: \bg-vben\src\components\AdList\src\components\ListFooter.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <Table
    v-if="summaryFunc || summaryData"
    :showHeader="false"
    :bordered="false"
    :pagination="false"
    :dataSource="getDataSource"
    :rowKey="(r) => r[rowKey]"
    :columns="getColumns"
    tableLayout="fixed"
    :scroll="scroll"
  />
</template>
<script lang="ts">
  import type { PropType } from 'vue';
  import { defineComponent, unref, computed, toRaw } from 'vue';
  import { Table } from 'ant-design-vue';
  import { isFunction } from '@/utils/is';
  import type { AdColumn } from '@/components/AdTable';
  import { propTypes } from '@/utils/propTypes';
  import { useListContext } from '../hooks/useListContext';

  const SUMMARY_ROW_KEY = '_row';
  const SUMMARY_INDEX_KEY = '_index';
  export default defineComponent({
    name: 'AdListFooter',
    components: { Table },
    props: {
      summaryFunc: {
        type: Function as PropType<Fn>,
      },
      summaryData: {
        type: Array as PropType<any[]>,
      },
      scroll: {
        type: Object as PropType<any>,
      },
      rowKey: propTypes.string.def('key'),
    },
    setup(props) {
      const INDEX_COLUMN_FLAG = 'INDEX';
      const list = useListContext();

      const getDataSource = computed((): any[] => {
        const { summaryFunc, summaryData } = props;
        if (summaryData?.length) {
          summaryData.forEach((item, i) => (item[props.rowKey] = `${i}`));
          return summaryData;
        }
        if (!isFunction(summaryFunc)) {
          return [];
        }
        let dataSource = toRaw(unref(list.getRawDataSource()));
        dataSource = summaryFunc(dataSource);
        dataSource.forEach((item, i) => {
          item[props.rowKey] = `${i}`;
        });
        return dataSource as any[];
      });

      const getColumns = computed(() => {
        const dataSource = unref(getDataSource);
        const columns: AdColumn[] = []; //  = cloneDeep(list.getColumns());
        const index = columns.findIndex((item) => item.flag === INDEX_COLUMN_FLAG);
        const hasRowSummary = dataSource.some((item) => Reflect.has(item, SUMMARY_ROW_KEY));
        const hasIndexSummary = dataSource.some((item) => Reflect.has(item, SUMMARY_INDEX_KEY));

        if (index !== -1) {
          if (hasIndexSummary) {
            columns[index].customRender = ({ record }) => record[SUMMARY_INDEX_KEY];
            columns[index].ellipsis = false;
          } else {
            Reflect.deleteProperty(columns[index], 'customRender');
          }
        }

        if (hasRowSummary) {
          const isFixed = columns.some((col) => col.fixed === 'left');
          columns.unshift({
            width: 60,
            title: 'selection',
            key: 'selectionKey',
            align: 'center',
            ...(isFixed ? { fixed: 'left' } : {}),
            customRender: ({ record }) => record[SUMMARY_ROW_KEY],
          });
        }
        return columns as any[];
      });
      return { getColumns, getDataSource };
    },
  });
</script>
